package com.aqie.easy.hash;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 1002. 查找常用字符
 * todo
 */
public class CommonChars {
    public List<String> commonChars(String[] A) {
        if (A == null || A.length == 0 || A.length == 1) return null;
        // 记录每个字符出现次数
        int[] hash = new int[26];
        // 是否第一次维护数组
        boolean firstFlag = true;
        for (String word : A){
            char[] wordChars = word.toCharArray();
            if (firstFlag){
                for (char wordChar : wordChars){
                    hash[wordChar - 'a']++;
                }
                firstFlag = false;
            }else{  // hash 数组有值
                int[] tmpHash = new int[26];
                for (char wordChar : wordChars){
                    tmpHash[wordChar - 'a']++;
                }

                for (int i = 0; i < hash.length; ++i){
                    // 比较两个数组,hash记录每次每个字符出现的最小次数
                    if (hash[i] > tmpHash[i]){
                        hash[i] = tmpHash[i];
                    }
                }

            }
        }
        // 组装返回结果
        List<String> res = new ArrayList<>();
        for(int i = 0; i < hash.length; ++i) {
            if(hash[i] != 0) {
                String tmp = String.valueOf((char)(i + 97));
                for(int j = 0; j < hash[i]; ++j) {
                    res.add(tmp);
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        String[] strs = {"bella","label","roller"};
        String str = "bella";
        Hashtable<Character, Integer> table = new Hashtable<>();
        for (int i =0 ;i < str.length(); i++){
            if (table.containsKey(str.charAt(i))){
                table.put(str.charAt(i), table.get(str.charAt(i)) + 1);
            }else {
                table.put(str.charAt(i), 1);
            }
        }
        System.out.println(table);

    }
}
